<?php
namespace kernel\library;

class Curl
{
    /**
     * get请求
     * @param string $url
     * @param array $data
     * @return mixed
     */
    public static function get(string $url, $data = '',  bool $resJson = false, array $header = [], $timeout = 30000)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if($data){
            if(is_array($data)){
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));
            }else{
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            }
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0');
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout);
        
        if($header){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        
        $output = curl_exec($ch);
        $curlInfo = curl_getinfo($ch);
        curl_close($ch);
        
        self::resultHand($url, $curlInfo, $output, $resJson);
        
        return $output;
    }
    
    /**
     * POST请求
     * @param string $url
     * @param array $data
     * @return mixed
     */
    public static function post(string $url, $data = '', bool $resJson = false, array $header = [], $timeout = 30000)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch, CURLOPT_POST, 1);
        if($data){
            if(is_array($data)){
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));
            }else{
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            }
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0');
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout);
        if($header){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        $output = curl_exec($ch);
        $curlInfo = curl_getinfo($ch);
        curl_close($ch);
        
        self::resultHand($url, $curlInfo, $output, $resJson);
        
        return $output;
    }
    
    /**
     * PUT请求
     * @param string $url
     * @param array $data
     * @return mixed
     */
    public static function put(string $url, $data = '',  bool $resJson = false, array $header = [], $timeout = 30000)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if($data){
            if(is_array($data)){
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));
            }else{
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            }
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0');
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout);
        
        if($header){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        
        $output = curl_exec($ch);
        $curlInfo = curl_getinfo($ch);
        curl_close($ch);
        
        self::resultHand($url, $curlInfo, $output, $resJson);
        
        return $output;
    }
    
    /**
     * DELETE请求
     * @param string $url
     * @param array $data
     * @return mixed
     */
    public static function delete(string $url, $data = '',  bool $resJson = false, array $header = [], $timeout = 30000)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        
        if($data){
            if(is_array($data)){
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));
            }else{
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            }
        }
        
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0');
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout);
        
        if($header){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        
        $output = curl_exec($ch);
        $curlInfo = curl_getinfo($ch);
        curl_close($ch);
        
        self::resultHand($url, $curlInfo, $output, $resJson);
        
        return $output;
    }
    
    
    /**
     * .结果处理
     * @param  $url
     * @param  $curlInfo
     * @param  $output
     * @param  $resJson
     */
    public static function resultHand($url, $curlInfo, &$output, $resJson){
        if($curlInfo['http_code'] == 0){
            Error::setError($url.'请求超时/域名不可访问', 408);
        }elseif($curlInfo['http_code'] > 399){
            Error::setError($url.'请求'.$curlInfo['http_code'].'错误'.$output, 419);
        }
        
        if($resJson == true){
            $output = json_decode($output, true);
            if(!is_array($output)){
				Error::setError($url.'返回的不是有效json数据', 419);
            }
        }
    }
    
}

